(:
   An XQuery script to enlighten interventions
  than contain a specific strings
  first parameter : the display file
  second parameter : a string

  The enlightened interventions use the color
      defined by the variable : $enlighten
  Variables $before and $after allow to
      select a numbers of items before and after
  The first item of a selection is indicated
      by a darker value, defined by the variable
      $darken
 :)

declare variable $enlighten := "lightred" ;
declare variable $darken := "black" ;
declare variable $before := -8;
declare variable $after := 8;
(:
  Don't change anything after this line ...
----------------------------------------------------- :)
declare variable $it := doc($arguments[1])/display/item;
declare variable $str := $arguments[2] ;
(: $lt : select items which contain the string :)
declare variable $lt :=
  for $e at $p in $it
  where ($e/info/text[fn:contains(./text(), $str)] ,
     $e/info/text[fn:contains(./text(), $str)]  )
  return $p;
(: $ln : select all lines before and after $lt :)
declare variable $ln :=
  for $i in $lt
    for $j in ($before to $after)
       return  $i+$j ;
(: $lb : lines to "darken" ; they should not be in $ln :)
declare variable $lb :=
  for $i in $lt
  where fn:empty(fn:index-of($ln, $i + $before - 1))
  return
     $i + $before - 1 ;
(: create the actual result :)
<display>{
  for $e at $p in $it
  where fn:exists(fn:index-of(($ln,$lb), $p))
  return
  if (fn:exists(fn:index-of($lt, $p)))
  then
    <item>{
       $e/info,
       <info name="defaultbgcolor"><color>{$enlighten}</color></info>
    }</item>
  else
  if (fn:exists(fn:index-of($lb, $p)))
  then
    <item>{
       $e/info,
       <info name="defaultbgcolor"><color>{$darken}</color></info>,
       <info name="signal">text:{$str}</info>
    }</item>
  else
   $e
}</display>
